﻿using B.S.BaseData.Api.Write.Applicantion.Command.RBAC;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Infrastructure.Interface;
using B.S.BataBase.Domain.RBAC;
using MediatR;

namespace B.S.BaseData.Api.Write.Applicantion.CommandHandler.RBAC
{
    public class GetPermissionHandler : IRequestHandler<GetPermissionCommand, APIResult<List<PermissionsModel>>>
    {
        private readonly IBaseRepository<PermissionsModel> permissionRepo;

        public GetPermissionHandler(IBaseRepository<PermissionsModel> permissionRepo)
        {
            this.permissionRepo = permissionRepo;
        }

        public Task<APIResult<List<PermissionsModel>>> Handle(GetPermissionCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<PermissionsModel>> res=new APIResult<List<PermissionsModel>>();
            var permission = permissionRepo.GetAll().Where(x => x.IsDeleted == false);
            if (!string.IsNullOrEmpty(request.PermissionsName))
            {
                permission = permission.Where(x=>x.PermissionsName.Contains(request.PermissionsName));
            }
            res.Code = ApiEnum.Success;
            res.Msg = "获取权限成功";
            res.Data=permission.ToList();
            return Task.FromResult(res);
        }
    }
}
